<template>
  <div class="home">
    <el-container class="container">
      <el-header>
        <div class="header-left">
          <img src="../assets/logo.png" alt="" />
          <span>电商后台管理系统</span>
        </div>
        <el-button type="info" @click="logout">退出登录</el-button></el-header
      >
      <el-container>
        <el-aside :width="isCollapse ? '64px' : '200px'">
          <!-- <el-row class="tac"> -->
          <div class="toggle-button" @click="toffleCollapse">|||</div>
          <el-menu
            :default-active="activePath"
            router
            :collapse-transition="false"
            :collapse="isCollapse"
            unique-opened
            default-active="2"
            class="el-menu-vertical-demo"
            background-color="#333744"
            text-color="#fff"
            active-text-color="#409eff"
          >
            <!-- 一级菜单 -->
            <el-submenu
              :index="item.id + ''"
              v-for="item in menulist"
              :key="item.id"
            >
              <!-- 一级菜单模板区域 -->
              <template slot="title">
                <i class="el-icon-location"></i>
                <span>{{ item.authName }}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item
                @click="saveNavState('/' + subitem.id)"
                :index="'/' + subitem.id"
                v-for="subitem in item.children"
                :key="subitem.id"
              >
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span>{{ subitem.authName }}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  name: "Home",
  data() {
    return {
      menulist: [
        {
          id: 101,
          authName: "用户管理",
          path: null,
          children: [
            {
              id: 1011,
              authName: "用户列表",
              path: null,
              children: [],
            },
          ],
        },
        {
          id: 102,
          authName: "权限管理",
          path: null,
          children: [
            {
              id: 1021,
              authName: "角色列表",
              path: null,
              children: [],
            },
            {
              id: 1022,
              authName: "权限列表",
              path: null,
              children: [],
            },
          ],
        },
        {
          id: 103,
          authName: "商品管理",
          path: null,
          children: [
            {
              id: 1031,
              authName: "商品列表",
              path: null,
              children: [],
            },
            {
              id: 1032,
              authName: "分类参数",
              path: null,
              children: [],
            },
            {
              id: 1033,
              authName: "商品分类",
              path: null,
              children: [],
            },
          ],
        },
        {
          id: 104,
          authName: "订单管理",
          path: null,
          children: [
            {
              id: 1041,
              authName: "订单列表",
              path: null,
              children: [],
            },
          ],
        },
        {
          id: 105,
          authName: "数据管理",
          path: null,
          children: [
            {
              id: 1051,
              authName: "商品列表",
              path: null,
              children: [],
            },
          ],
        },
      ],
      isCollapse: false,
      activePath: "",
    };
  },
  created() {
    this.activePath = window.sessionStorage.getItem("activePath");
    // console.log(this.activePath)
  },
  methods: {
    logout() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    toffleCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    saveNavState(activePath) {
      window.sessionStorage.setItem("activePath", activePath);
      this.activePath = activePath;
    },
  },
};
</script>

<style scoped>
.home {
  height: 100%;
}
.header-left {
  color: #fff;
  font-size: 20px;
}
.header-left img {
  height: 40px;
  width: 40px;
  vertical-align: middle;
}
.container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.el-aside {
  background-color: #333744;
}
.el-main {
  background-color: #eaedf1;
}
.el-menu {
  border-right: none;
}
.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>